package com.kaifamiao.www.Thread.do0926;

public class Ticket implements Runnable {
    //总票数
    private int ticket = 100;

    Object obj = new  Object();
    /**
     * 执行卖票操作
     */
    @Override
    public void run() {
        //窗口永远开启
        while (true) {
            //有票可卖
            //sleep模拟出票时间
            synchronized (obj){
                if (ticket > 0) {
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //获取当前线程的名字
                    String name = Thread.currentThread().getName();
                    System.out.println(name + "正在卖第" + ticket-- +"张票");
                }
            }

        }
    }
}